<?php

namespace app\admin\controller;

use app\models\Admin;
use app\validates\Admin as Validate;

Class Index extends Base
{
    public function login()
    {
        $admin_id = session('admin_id');
        if (Admin::get($admin_id)) {
            return redirect('/main/index');
        }
        $data = $this->request->post();
        if ($this->request->isPost()) {
            $validate = new Validate();
            if (!$validate->scene('login')->check($data)) {
                return json(['status' => 1, 'msg' => $validate->getError()]);
            }

            $admin = Admin::get(['username' => $data['username']]);
            if ($admin == false) {
                return json(['status' => 1, 'msg' => '账号密码错误']);
            }
//            return json(['status' => 1, 'msg' => '断点2']);
//            return md5(md5($data['password']) . md5($admin['entry']));
            if ($admin['password'] != md5(md5($data['password']) . md5($admin['entry']))) {
                return json(['status' => 1, 'msg' => '账号或者密码错误']);
            }

            session('admin_id', $admin['id']);
//            $admin->update([
//                'login_num' => $admin['login_num'] + 1,
//                'login_ip' => request()->ip(),
//            ], [
//                'id' => $admin['id']
//            ]);

            return json(['status' => 0, 'msg' => '登陆成功', 'url' => url('main/index')]);
        }

        return view(null, [
            'data' => $data
        ]);
    }

    public function logout()
    {
        session('admin_id', null);
        $this->redirect('index/login');
    }

}